package com.ibeetl.admin.core.util;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class DateUtil {
    public static final String PATTERN_FULL_DATE_TIME_24 = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_FULL_DATE_DASH = "yyyy-MM-dd";
    public static final String PATTERN_SHORT_DATE_SLASH = "M/d/yyyy";
    public static final String PATTERN_PERIOD_DATE = "yyyyMM";
    public static final String PATTERN_MONTH_YEAR = "MM/yyyy";
    public static final String PATTERN_YEAR_MONTH = "yyyy/MM";

    public static final String PATTERN_FULL_DATE_SLASH = "MM/dd/yyyy";
    public static final String PATTERN_FULL_DATE_SLASH_1 = "yyyy/MM/dd";
    public static final String PATTERN_FULL_DATE_DOT = "yyyy.MM.dd";
    public static final String PATTERN_FULL_DATE = "yyyyMMdd";
    public static final String DATE_PATTERN_DASH_1 = "MM-dd-yyyy";
    public static final String DATE_TIME_PATTERN_2 = "MM/dd/yyyy HH:mm:ss";
    public static final String PATTERN_FULL_DATE_TIME_UNDERSCORE = "yyyyMMdd_HHmmss";
    public static Date MAX_DATE = maxDate();

    private static Set<String> patterns_datetime = new HashSet();
    private static Set<String> patterns_date = new HashSet();

    static {
        patterns_date.add(DateUtil.PATTERN_FULL_DATE_DASH);
        patterns_date.add(DateUtil.PATTERN_FULL_DATE_DOT);
        patterns_date.add(DateUtil.PATTERN_FULL_DATE_SLASH_1);
        patterns_date.add(DateUtil.PATTERN_FULL_DATE_SLASH);
        patterns_date.add(DateUtil.PATTERN_FULL_DATE);
        patterns_date.add(DateUtil.PATTERN_SHORT_DATE_SLASH);
        patterns_date.add(DateUtil.PATTERN_PERIOD_DATE);
        patterns_date.add(DateUtil.PATTERN_MONTH_YEAR);
        patterns_date.add(DateUtil.DATE_PATTERN_DASH_1);
        patterns_datetime.add(DateUtil.DATE_TIME_PATTERN_2);
        patterns_datetime.add(DateUtil.PATTERN_FULL_DATE_TIME_24);
        patterns_datetime.add(DateUtil.PATTERN_FULL_DATE_TIME_UNDERSCORE);
    }

    public static Date string2Date(String date) {
        if(!StringUtils.hasText(date)) return null;
        Date dateObj;
        Iterator it = patterns_date.iterator();
        if(date.length() > 10) {
            it = patterns_datetime.iterator();
        }
        while (it.hasNext()) {
            String pattern = (String) it.next();
            dateObj = string2Date(date,pattern,false);
            if(dateObj != null) {
                return dateObj;
            }
        }
        return string2Date(date, PATTERN_FULL_DATE_SLASH);
    }

    public static Date string2Date(String originalValue, String format) {
        return string2Date(originalValue, format, true);
    }

    public static Date string2Date(String originalValue, String format, boolean lenient) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setLenient(lenient);
        try {
            return dateFormat.parse(originalValue);
        } catch (ParseException e) {
            return null;
        }
    }

    public static String date2String(Date originalValue) {
        return date2String(originalValue, PATTERN_FULL_DATE_DASH);
    }

    public static String date2String(Date originalValue, String format) {
        if (originalValue == null) return null;

        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(originalValue);
    }
    
    public static String now() {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_FULL_DATE_DASH);
        return sdf.format(new Date());
    }
    
    public static String now(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }
    
    private static Date maxDate() {
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_FULL_DATE_TIME_24);
        try {
            return sdf.parse("9999-12-31 23:59:59");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
